package org.web.accesscontrollist.server.biz.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.web.accesscontrollist.sdk.dto.AclResourceDetailDTO;
import org.web.accesscontrollist.sdk.helper.AclHelper;
import org.web.accesscontrollist.server.domain.AclResourceDetailDO;
import org.web.accesscontrollist.server.service.AclResourceDetailService;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("saveBatchBiz")
public class SaveBatchBizImpl {

    @Resource
    private AclResourceDetailService aclResourceDetailService;

    @Transactional
    public void saveBatch(List<AclResourceDetailDO> insertList, List<AclResourceDetailDO> updateList, Map<String, AclResourceDetailDO> mapFromDB, Map<String, AclResourceDetailDTO> resourceDetailDTOMap) {
        aclResourceDetailService.insertAclResourceDetailBatch(insertList);
        aclResourceDetailService.updateAclResourceDetailDOBatch(updateList);
        for (AclResourceDetailDO aclResourceDetailDO : insertList) {
            supplementReturn(aclResourceDetailDO, resourceDetailDTOMap, mapFromDB);
        }

        for (AclResourceDetailDO aclResourceDetailDO : updateList) {
            supplementReturn(aclResourceDetailDO, resourceDetailDTOMap, mapFromDB);
        }
    }


    /**
     * 定义了一个名为 supplementReturn 的方法，
     * 该方法接收三个参数：
     * aclResourceDetailDO（类型为 AclResourceDetailDO）、
     * resourceDetailDTOMap（类型为 Map<String, AclResourceDetailDTO>）和
     * mapFromDB（类型为 Map<String, AclResourceDetailDO>）
     */
    private void supplementReturn(AclResourceDetailDO aclResourceDetailDO, Map<String, AclResourceDetailDTO> resourceDetailDTOMap, Map<String, AclResourceDetailDO> mapFromDB) {
        String keyName = AclHelper.buildUniqueKeyByAttributes(aclResourceDetailDO.getBusinessLine(), aclResourceDetailDO.getTypeName(), aclResourceDetailDO.getDetailName());
        AclResourceDetailDTO aclResourceDetailDTO = resourceDetailDTOMap.get(keyName);
        /*通过检查 fromDB 是否为非空，来决定如何填充 aclResourceDetailDTO 对象的属性。
         * 如果 fromDB 非空，则将其 createTime 和 isDelete 属性值赋给 aclResourceDetailDTO 对象；
         * 否则，直接将 aclResourceDetailDO 对象的 createTime 和 isDelete 属性值赋给 aclResourceDetailDTO 对象。*/
        AclResourceDetailDO fromDB = mapFromDB.get(keyName);
        if (fromDB != null) {
            aclResourceDetailDTO.setCreateTime(fromDB.getCreateTime());
            aclResourceDetailDTO.setIsDelete(fromDB.getIsDelete());
        } else {
            aclResourceDetailDTO.setCreateTime(aclResourceDetailDO.getCreateTime());
            aclResourceDetailDTO.setIsDelete(aclResourceDetailDO.getIsDelete());
        }
        /*
         * 设置 aclResourceDetailDTO 对象的其他属性：
         * executeSuccess 为 true、UpdateTime 为 aclResourceDetailDO 的 updateTime 属性值、
         * aclResourceDetailId 为 aclResourceDetailDO 的 aclResourceDetailId 属性值。
         */
        aclResourceDetailDTO.setExecuteSuccess(true);
        aclResourceDetailDTO.setUpdateTime(aclResourceDetailDO.getUpdateTime());
        aclResourceDetailDTO.setAclResourceDetailId(aclResourceDetailDO.getAclResourceDetailId());
    }
}
